<?php
namespace app\controller\back;

use app\common\controller\_AdminController;
use app\model\Config;
use think\Validate;

class ConfigController extends _AdminController
{	
	protected $rule = [
		'id|编号'			=> 'integer',
		'alias|别名'			=> 'alphaDash|length:1,50',
		'name|名称'      	=> 'chsDash|length:1,50',
		'sort|排序'          => 'integer',
		'desc|描述'          => 'chsDash|max:200',
		'type|类型'			=> 'in:number,string,array,json',
		'value|值'			=> 'min:1',
		'page|页码'         	=> 'integer',
        'size|条数'        	=> 'integer',
	];
	
	protected $scene = [
		'create'			=> ['alias','name','sort','desc','value','type'],
		'update'			=> ['id','alias','name','sort','desc','value','type'],
        'delete'            => ['id'],
        'list'              => ['page','size'],
	];

	protected function initialize()
	{	
		parent::initialize();
		switch ($this->action) {
			case 'create':
				$this->appendRule('alias','require|unique:config');
				$this->appendRule('name','require|unique:config');
				$this->appendRule('type','require');
				$this->appendRule('value','require');
				break;
			case 'update':
				$this->appendRule('id','require|checkConfigById');
				$this->appendRule('alias','unique:config');
				$this->appendRule('name','unique:config');
				break;
            case 'delete':
                $this->appendRule('id','require|checkConfigById');
                break;
			default:
				# code...
				break;
		}
		$this->check();
	}
	/**
	 * @api {POST} /back/config/create 添加配置
	 * @apiVersion 0.0.1
	 * @apiGroup Back-config
	 *
	 * @apiParam {String} alias 别名
	 * @apiParam {String} name 名称
	 * @apiParam {Number} [sort=0] 排序
	 * @apiParam {String} [desc] 描述
	 * @apiParam {String} value 值
	 * @apiParam {String} type 类型
	 *
	 * @apiParamExample {json} Request-Example:
	 * {
	 *     "alias": "zcx",
	 *     "name": "ad222",
	 *     "sort": "1",
	 *     "desc": "asds",
	 *     "value": "1",
     *     "type": "number",   
	 * }
	 * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": []
	 * }
	 */
	public function createAction()
	{   
		$error = Config::doSave($this->data);
		if($error){
            parent::resError($error);
        }
        return parent::resSuccess();
	}


	/**
	 * @api {POST} /back/config/update 修改配置
	 * @apiVersion 0.0.1
	 * @apiGroup Back-config
	 *
	 * @apiParam {Number} id 编号
	 * @apiParam {String} [alias] 别名
	 * @apiParam {String} [name] 名称
	 * @apiParam {Number} [sort=0] 排序
	 * @apiParam {String} [desc] 描述
	 * @apiParam {String} [value] 值
	 * @apiParam {String} [type] 类型
     *
     * @apiParamExample {json} Request-Example:
     * {
	 *     "alias": "zcx",
	 *     "name": "ad222",
	 *     "sort": "1",
	 *     "desc": "asds",
	 *     "value": "1",
     *     "type": "number",    
     * }
     * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": []   
	 * }
	 */
	public function updateAction()
	{   
		$error = Config::doSave($this->data,$this->id);
		if($error){
            parent::resError($error);
        }
        return parent::resSuccess();
	}

	/**
	 * @api {POST} /back/config/delete 删除配置
	 * @apiVersion 0.0.1
	 * @apiGroup Back-config
	 *
	 * @apiParam {Number} id 编号
     *
     * @apiParamExample {json} Request-Example:
     * {
     *     "id": "1",
     * }
     * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": []   
	 * }
	 */
	public function deleteAction()
	{   
		Config::doDelete($this->id);
        return parent::resSuccess();
	}

	/**
	 * @api {POST} /back/config/list 配置列表
	 * @apiVersion 0.0.1
	 * @apiGroup Back-config
	 * 
	 * @apiSuccess {Number} id 编号
	 * @apiSuccess {String} alias 别名
	 * @apiSuccess {String} name 名称
	 * @apiSuccess {Number} sort 排序
	 * @apiSuccess {String} desc 描述
	 * @apiSuccess {String} value 值
	 * @apiSuccess {String} type 类型
	 * @apiSuccess {String} create_time 创建时间
	 * @apiSuccess {String} update_time 更新时间
	 * @apiSuccess {String} count 总条数
     * 
	 * @apiSuccessExample {json} 成功返回:
	 * HTTP/1.1 200 OK
	 * {
	 *     "code":200,
	 *     "msg":"操作成功",
	 *     "data": [{
     *         "id": 39,
	 *         "alias": "zcx",
	 *         "name": "ad222",
	 *         "sort": "1",
	 *         "desc": "asds",
	 *         "value": "1",
     *         "type": "number",  
     *         "create_time": "2019-04-09 17:24:19",
     *         "update_time": "2019-04-09 17:24:19",
	 *     }],
	 *     "count": 6
	 * }
	 */
	public function listAction()
	{   
		$list = Config::doList([],$this->page,$this->size);
		return parent::resSuccess($list);
	}
	
	

	// 自定义规则
	protected function checkConfigById()
	{
		Validate::extend('checkConfigById', function ($value) {
			$config = Config::get($value);
			if(!$config){
				return '记录不存在';
			}
			return true;
		});
	}
}
